home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 June / EnigmA AMIGA RUN 08 (1996)(G.R. Edizioni)(IT)[!][issue 1996-06][EARSAN CD VII].iso / earcd / utilsys / rss14gmd.lha / RSys_1.4gmd / C / Search.c < prev    next >
C/C++ Source or Header  |  1996-05-04  |  9KB  |  421 lines

  1. /*
  2.    ***************************************************************************
  3.    *
  4.    * Datei:
  5.    *    RSysSearch.c
  6.    *
  7.    * Inhalt:
  8.    *
  9.    *      --- Globale Routinen ---
  10.    *
  11.    *    void RSysFindNext ( void );
  12.    *    void RSysFindPrev ( void );
  13.    *    void RSysSearch ( void );
  14.    *
  15.    *      --- Lokale  Routinen ---
  16.    *
  17.    *    static int HandleSearchStrIDCMP ( void );
  18.    *    static int OpenSearchStrWindow ( void );
  19.    *    static int SearchCancelGadClicked ( void );
  20.    *    static int SearchOkGadClicked ( void );
  21.    *    static int SearchSGadClicked ( void );
  22.    *    static int SearchStrCloseWindow ( void );
  23.    *    static int SearchStrVanillaKey ( void );
  24.    *    static void EnterSearchString ( void );
  25.    *    static void InfoMsg ( char *fmt );
  26.    *    static void StrFound ( NODE *node );
  27.    *    static void StrNotFound ( int num );
  28.    *
  29.    * Bemerkungen:
  30.    *    Enthält die Routinen zum Suchen nach einem Teilstring
  31.    *    in der Liste des Hauptfensters.
  32.    *
  33.    * Erstellungsdatum:
  34.    *    25-Sep-93    Rolf Böhme
  35.    *
  36.    * Änderungen:
  37.    *    25-Sep-93    Rolf Böhme    Erstellung
  38.    *
  39.    ***************************************************************************
  40.  */
  41.  
  42. #include "RSys.h"
  43. #include "protos.h"
  44.  
  45. static int SearchSGadClicked (void);
  46. static int SearchOkGadClicked (void);
  47. static int SearchCancelGadClicked (void);
  48. static int HandleSearchStrIDCMP (void);
  49. static int SearchStrCloseWindow ();
  50. static int SearchStrVanillaKey ();
  51. static int OpenSearchStrWindow (void);
  52. static void EnterSearchString (void);
  53.  
  54.  
  55. static char searchstr[BUFSIZE];
  56. static startsearch = FALSE;
  57.  
  58. static WINDOW *SearchStrWnd = NULL;
  59. static GADGET *SearchStrGList = NULL;
  60. INTUIMESSAGE SearchStrMsg;
  61. static GADGET *SearchStrGadgets[SearchStr_CNT];
  62. static UWORD SearchStrLeft = 81;
  63. static UWORD SearchStrTop = 66;
  64. static UWORD SearchStrWidth = 259;
  65. static UWORD SearchStrHeight = 57;
  66. static UBYTE *SearchStrWdt = (UBYTE *) NAME " - Search string";
  67.  
  68. static UWORD SearchStrGTypes[] =
  69. {
  70.   STRING_KIND,
  71.   BUTTON_KIND,
  72.   BUTTON_KIND
  73. };
  74.  
  75. static NEWGADGET SearchStrNGad[] =
  76. {
  77.   22, 15, 208, 13, (UBYTE *) "Search String", NULL, GD_SearchSGad, PLACETEXT_ABOVE, NULL, (APTR) SearchSGadClicked,
  78.   22, 34, 91, 13, (UBYTE *) "_Search", NULL, GD_SearchOkGad, PLACETEXT_IN, NULL, (APTR) SearchOkGadClicked,
  79.   139, 34, 91, 13, (UBYTE *) "Cancel", NULL, GD_SearchCancelGad, PLACETEXT_IN, NULL, (APTR) SearchCancelGadClicked
  80. };
  81.  
  82. static ULONG *SearchStrGTags[] =
  83. {
  84.   (ULONG *) (GTST_MaxChars), (ULONG *) 40, (ULONG *) (TAG_DONE),
  85.   (ULONG *) (GT_Underscore), (ULONG *) '_', (ULONG *) (TAG_DONE),
  86.   (ULONG *) (TAG_DONE)
  87. };
  88.  
  89. static int
  90. SearchSGadClicked (void)
  91. {
  92.   char *gadtext;
  93.  
  94.   /* routine when gadget "Search String" is clicked. */
  95.   gadtext = gadgetbuff (SearchStrGadgets[GD_SearchSGad - GD_SearchSGad]);
  96.  
  97.   if (strlen (gadtext) != 0)
  98.     {
  99.       strncpy (searchstr, gadtext, BUFSIZE);
  100.       startsearch = TRUE;
  101.  
  102.       return TRUE;
  103.     }
  104.  
  105.   searchstr[0] = STRINGEND;
  106.  
  107.   startsearch = FALSE;
  108.  
  109.   return TRUE;
  110. }
  111.  
  112. static int
  113. SearchOkGadClicked (void)
  114. {
  115.   /* routine when gadget "_Search" is clicked. */
  116.   startsearch = TRUE;
  117.  
  118.   return FALSE;
  119. }
  120.  
  121. static int
  122. SearchCancelGadClicked (void)
  123. {
  124.   /* routine when gadget "Cancel" is clicked. */
  125.   startsearch = FALSE;
  126.  
  127.   return FALSE;
  128. }
  129.  
  130. static int
  131. SearchStrCloseWindow (void)
  132. {
  133.   /* routine for "IDCMP_CLOSEWINDOW". */
  134.   startsearch = FALSE;
  135.  
  136.   return FALSE;
  137. }
  138.  
  139. static int
  140. SearchStrVanillaKey (void)
  141. {
  142.   /* routine for "IDCMP_VANILLAKEY". */
  143.  
  144.   if (SearchStrMsg.Code == ESC)
  145.     {
  146.       startsearch = FALSE;
  147.  
  148.       return FALSE;
  149.     }
  150.  
  151.   if (ToUpper ((ULONG) SearchStrMsg.Code) == (UBYTE) 'S')
  152.     {
  153.       startsearch = TRUE;
  154.  
  155.       return FALSE;
  156.     }
  157.  
  158.   return TRUE;
  159. }
  160.  
  161. static int
  162. HandleSearchStrIDCMP (void)
  163. {
  164.   INTUIMESSAGE *m;
  165.   int (*func) (void);
  166.   BOOL running = TRUE;
  167.  
  168.   while (m = GT_GetIMsg (SearchStrWnd->UserPort))
  169.     {
  170.       CopyMem ((char *) m, (char *) &SearchStrMsg,
  171.            (long) sizeof (INTUIMESSAGE));
  172.  
  173.       GT_ReplyIMsg (m);
  174.  
  175.       switch (SearchStrMsg.Class)
  176.     {
  177.     case IDCMP_REFRESHWINDOW:
  178.       MakeWindowRefresh (SearchStrWnd);
  179.       break;
  180.  
  181.     case IDCMP_CLOSEWINDOW:
  182.       running = SearchStrCloseWindow ();
  183.       break;
  184.  
  185.     case IDCMP_VANILLAKEY:
  186.       running = SearchStrVanillaKey ();
  187.       break;
  188.  
  189.     case IDCMP_GADGETUP:
  190.       HandleHelp ((enum RSysNumber)
  191.               ((GADGET *) SearchStrMsg.IAddress)->GadgetID);
  192.  
  193.       func = (int (*)()) ((GADGET *) SearchStrMsg.IAddress)->UserData;
  194.       running = func ();
  195.       break;
  196.     }
  197.     }
  198.  
  199.   return (running);
  200. }
  201.  
  202. static int
  203. OpenSearchStrWindow (void)
  204. {
  205.   NEWGADGET ng;
  206.   GADGET *g;
  207.   UWORD lc, tc;
  208.   UWORD wleft = SearchStrLeft, wtop = SearchStrTop, ww, wh;
  209.   int gl[] =
  210.   {GD_SearchSGad - GD_SearchSGad};
  211.  
  212.   AdjustWindowDimensions (Scr, SearchStrLeft, SearchStrTop,
  213.               SearchStrWidth, SearchStrHeight,
  214.               &wleft, &wtop, &ww, &wh);
  215.  
  216.   if (!(g = CreateContext (&SearchStrGList)))
  217.     return 1L;
  218.  
  219.   for (lc = 0, tc = 0; lc < SearchStr_CNT; lc++)
  220.     {
  221.  
  222.       CopyMem ((char *) &SearchStrNGad[lc], (char *) &ng,
  223.            (long) sizeof (NEWGADGET));
  224.  
  225.       ng.ng_VisualInfo = VisualInfo;
  226.       ng.ng_TextAttr = Font;
  227.       ng.ng_LeftEdge = OffX + ComputeX (ng.ng_LeftEdge);
  228.       ng.ng_TopEdge = OffY + ComputeY (ng.ng_TopEdge);
  229.       ng.ng_Width = ComputeX (ng.ng_Width);
  230.       ng.ng_Height = ComputeY (ng.ng_Height);
  231.  
  232.       SearchStrGadgets[lc] = g = CreateGadgetA ((ULONG) SearchStrGTypes[lc], g, &ng, (TAGITEM *) & SearchStrGTags[tc]);
  233.  
  234.       while (SearchStrGTags[tc])
  235.     tc += 2;
  236.       tc++;
  237.  
  238.       if (NOT g)
  239.     return 2L;
  240.     }
  241.  
  242.   if (!(SearchStrWnd = OpenWindowTags (NULL,
  243.                        WA_Left, wleft,
  244.                        WA_Top, wtop,
  245.                        WA_Width, ww,
  246.                        WA_Height, wh,
  247.                        WA_IDCMP, STRINGIDCMP | BUTTONIDCMP | IDCMP_CLOSEWINDOW | IDCMP_VANILLAKEY | IDCMP_REFRESHWINDOW,
  248.                        WA_Flags, WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_SMART_REFRESH | WFLG_ACTIVATE | WFLG_RMBTRAP,
  249.                        WA_Title, SearchStrWdt,
  250.                        WA_PubScreenFallBack, TRUE,
  251.                        WA_PubScreen, Scr,
  252.                        TAG_DONE)))
  253.     return 4L;
  254.  
  255.   RefreshRastPort (SearchStrWnd, SearchStrGadgets, gl, 1, FALSE, SearchStrGList);
  256.  
  257.   return NULL;
  258. }
  259.  
  260. static void
  261. InfoMsg (char *fmt)
  262. {
  263.   char msg[BUFSIZE];
  264.  
  265.   sprintf (msg, fmt, searchstr);
  266.   PrintInfo (msg, SPEAK, 30);
  267.  
  268.   return;
  269. }
  270.  
  271. static void
  272. StrNotFound (int num)
  273. {
  274.   actualfindnode = GetNode (&ListeLVList, (ULONG) num);
  275.   actualfindnodenum = num;
  276.  
  277.   topentry = actualfindnodenum;
  278.   SetMainLVTop (actualfindnodenum);
  279.  
  280.   InfoMsg ("\"%s\" not found...");
  281.  
  282.   PrintStatistics ();
  283.  
  284.   return;
  285. }
  286.  
  287. static void
  288. StrFound (NODE * node)
  289. {
  290.   actualfindnode = node;
  291.  
  292.   topentry = actualfindnodenum;
  293.   SetMainLVTop (topentry);
  294.  
  295.   InfoMsg ("\"%s\" found...");
  296.  
  297.   PrintStatistics ();
  298.  
  299.   return;
  300. }
  301.  
  302. void
  303. RSysFindNext (void)
  304. {
  305.   NODE *node;
  306.   int lastnum = actualfindnodenum;
  307.  
  308.   HandleHelp (MN_FindNext);
  309.  
  310.   if (actualfindnodenum >= (countentries - newlvh))
  311.     return;
  312.  
  313.   if (strlen (searchstr) == 0)
  314.     {
  315.       EnterSearchString ();
  316.  
  317.       if (!startsearch)
  318.     return;
  319.     }
  320.  
  321.   InfoMsg ("Find next \"%s\"...");
  322.  
  323.   for (node = actualfindnode->ln_Succ, actualfindnodenum++;
  324.        node->ln_Succ; node = node->ln_Succ, actualfindnodenum++)
  325.     if (strstr (node->ln_Name, searchstr))
  326.       {
  327.     StrFound (node);
  328.  
  329.     return;
  330.       }
  331.  
  332.   StrNotFound (lastnum);
  333.  
  334.   return;
  335. }
  336.  
  337. void
  338. RSysFindPrev (void)
  339. {
  340.   NODE *node;
  341.   int lastnum = actualfindnodenum;
  342.  
  343.   HandleHelp (MN_FindPrev);
  344.  
  345.   if (actualfindnodenum <= 0)
  346.     return;
  347.  
  348.   if (strlen (searchstr) == 0)
  349.     {
  350.       EnterSearchString ();
  351.  
  352.       if (!startsearch)
  353.     return;
  354.     }
  355.  
  356.   InfoMsg ("Find prev: \"%s\"...");
  357.  
  358.   actualfindnodenum--;
  359.  
  360.   for (node = actualfindnode->ln_Pred; node->ln_Pred; node = node->ln_Pred, actualfindnodenum--)
  361.     if (strstr (node->ln_Name, searchstr))
  362.       {
  363.     StrFound (node);
  364.     return;
  365.       }
  366.  
  367.   StrNotFound (lastnum);
  368.  
  369.   return;
  370. }
  371.  
  372. static void
  373. EnterSearchString (void)
  374. {
  375.   PrintInfo ("Search string...", SPEAK, 0);
  376.  
  377.   if (OpenASysWindow (OpenSearchStrWindow, NO_KILL))
  378.     {
  379.       LockMainWindow (WIN_LOCK);
  380.  
  381.       GT_SetGadgetAttrs (SearchStrGadgets[GD_SearchSGad - GD_SearchSGad], SearchStrWnd,
  382.              NULL,
  383.              GTST_String, (UBYTE *) searchstr,
  384.              TAG_DONE);
  385.  
  386.       ActivateGadget (SearchStrGadgets[GD_SearchSGad - GD_SearchSGad], SearchStrWnd, NULL);
  387.  
  388.       while (HandleSearchStrIDCMP ());
  389.  
  390.       CloseASysWindow (&SearchStrWnd, &SearchStrGList, NULL);
  391.  
  392.       LockMainWindow (WIN_UNLOCK);
  393.     }
  394.  
  395.   return;
  396. }
  397.  
  398. void
  399. RSysSearch (void)
  400. {
  401.   HandleHelp (MN_Search);
  402.  
  403.   EnterSearchString ();
  404.  
  405.   if (startsearch && strlen (searchstr) == 0L)
  406.     startsearch = FALSE;
  407.  
  408.   if (startsearch)
  409.     {
  410.       startsearch = FALSE;
  411.       actualfindnode = ListeLVList.lh_Head;
  412.       actualfindnodenum = 0;
  413.  
  414.       RSysFindNext ();
  415.     }
  416.  
  417.   PrintStatistics ();
  418.  
  419.   return;
  420. }
  421.